// pages/mine/bind.js
const IDCard = require('../../utils/idcard.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idcardfocus:false,
    namefocus:false,
    idcard:null,
    username:null,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop
    })
  },
  checkUserName: function (e) {
    var username = e.detail.value;
    username = username.trim();
    //console.log(username)
    if (username == "") {
      //console.log(username)
      wx.showToast({
        title: '姓名不能为空',
        image: '../../icon/fail.png'
      })
      this.setData({
        namefocus: true
      })
    }else{
      this.setData({
        username: username
      })
    }

  },
  inputIdCard: function (e) {
    let idcard = e.detail.value;
    console.log(idcard);
    if (idcard) {
      if (idcard.length == 18) {
        var retrun = IDCard.IdCard(idcard, 4);
        if (retrun == "error") {
          wx.showToast({
            title: '身份证号码不正确',
            image: '../../icon/fail.png'
          })
          this.setData({
            idcardfocus: true
          })
        }else{
          this.setData({
            idcard: idcard
          })
        } 
      } else {
        wx.showToast({
          title: '身份证号码长度不对',
          image: '../../icon/fail.png'
        })
        this.setData({
          idcardfocus: true
        })
      }
    }
  },
  inputIdCard2: function (e) {
    let idcard = e.detail.value;
    //console.log(idcard);
    if (idcard) {
      if (idcard.length == 18) {
        var retrun = IDCard.IdCard(idcard, 4);
        if (retrun == "error") {
          wx.showToast({
            title: '身份证号码不正确',
            image: '../../icon/fail.png'
          })
          this.setData({
            idcardfocus: true
          })
        } else {
          this.setData({
            idcard: idcard
          })
          //console.log(retrun);
        }
      }
    }
  },
  bindMember:function(){
    var username=this.data.username;
    var idcard=this.data.idcard;
    var that=this;
    var open=wx.getStorageSync('open')
    wx.request({
      url: app.globalData.domain+'member/member/bindMember',
      data: { username: username,idcard: idcard,unionid: open.unionid,id:open.id},
      success:function(res){
      console.log(res);
       let d= res.data;
       console.log(d)
        if (res.data.status==200){
          open.member_id=d.member_id;
          open.bind=1;
          wx.setStorageSync('open',open)
          app.showModals(d.msg,'pages/index/index','index')

        }else{
            app.showModals(d.msg,'#','#')
        }
        //that.setData{{}}
      }

    })
  },
  goBack:function(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})